<?php
namespace Api\Controller;
use Think\Controller;
class IndexController extends PublicController {
	//***************************
	//  首页数据接口
	//***************************
    public function index(){
    	//如果缓存首页没有数据，那么就读取数据库
    	$ggtop=M('guanggao')
                 ->order('sort desc,id asc')
                   ->field('id,name,photo,type')
                     ->limit(5)
                       ->select();
		foreach ($ggtop as $k => $v) {
			$ggtop[$k]['photo']=__DATAURL__.$v['photo'];
			$ggtop[$k]['name']=$v['name'];
		}
        $brand = M('brand')
                   ->where('if_show=1 AND 1=1 ')
                     ->field('id,name')
                         ->select();
        foreach ($brand as $k => $v) {
            $find_data = M("brand_image")->where(" brand_id=".$v['id'])->order("image_order asc")->limit(1)->select();
            $find_data = current($find_data);
            $brand[$k]['brand_photo'] = __DATAURL__."UploadFiles/".$find_data['brand_photo'];
        }
    	$this->json_result(  array(
            'ggtop'=>$ggtop,
            'brand'=>$brand
        ));
    	exit();
    }


    /**
     * 测试缓存
     */
    public function set_cache()
    {
        $data = array(
            'id' => "1",
            'name' => "xiaoming",
            "fff"  => "cdsdssffesfes"
        );
        $this->setUserInfo($data);


    }
    /**
     * 获取redis的key值
     *
     * @param string $key | 键
     * @author wechan
     * @date 2016年05月03日
     */
    public function geteee(){
        echo $this->get("id");
    }

    //***************************
    //  首页产品 分页
    //***************************
    public function getlist(){
        $page = intval($_REQUEST['page']);
        $limit = intval($page*8)-8;

        $pro_list = M('product')->where('del=0 AND pro_type=1 AND is_down=0 AND type=1')->order('sort desc,id desc')->field('id,name,photo_x,price_yh,shiyong')->limit($limit.',8')->select();
        foreach ($pro_list as $k => $v) {
            $pro_list[$k]['photo_x'] = __DATAURL__.$v['photo_x'];
        }

        echo json_encode(array('prolist'=>$pro_list));
        exit();
    }

    public function ceshi(){
        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($strPol)-1;

        for($i=0;$i<32;$i++){
            $str.=$strPol[rand(0,$max)];//rand($min,$max)生成介于min和max两个数之间的一个随机整数
        }

        echo $str;
    }

    public function test()
    {
        echo $this->get("id");
    }

    public function del()
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);
        $res = $redis->del($this->t_session);
        if ( $res ) {
            echo "test";
        } else {
            echo "fail";
        }
    }

    /**
     * 获取最后一个订单id
     *
     * @author    Doujia
     * @return    string
     */
    public function gen_last_order_id()
    {
        //获取最后一个订单
        $order_model =M("order");
        $data = $order_model->order('id desc')->limit(0,1)->getField("id")+1;
        echo $data;
    }

    public function orderhandle(){
        //$order_sn = trim($data['order_sn']);
        $pay_type = 'weixin';
        $trade_no = "4200000062201802057741105850";
        $order_sn = "2018020595152_77";
        $outTradeNoArray = explode('_',$order_sn);
        $out_trade_nos = $outTradeNoArray[0];
        $map['order_sn'] = ["like","%$out_trade_nos%"];
        $order_infos = M('order')->where($map)->select();
        foreach ( $order_infos as $key=>$value ) {
            echo $value['order_sn'];
            if ($value['status']<10 || $value['back']>'0') {
                return "订单异常...";
            }
            $up = array();
            $up['type'] = $pay_type;
            $up['status'] = 20;
            $up['trade_no'] = $trade_no;
            $res = M('order')->where('order_sn="'.$value['order_sn'].'"')->save($up);
        }
    }


}